package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


//角色管理
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    //增加
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds,@RequestBody Role role){
        try {

            System.out.println("============================"+role+",,,"+permissionIds+"===========================");
            roleService.add(role,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
        return new Result(true,"添加角色成功");
    }
    @RequestMapping("/findAllp")
    public Result findAllp(){
        try {
            List<Permission> list= roleService.findAllp();
            return new Result(true,"查询权限成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限失败");
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,"查询检查组成功",role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询检查组失败");
        }

    }

    //在编辑页面回显权限数据
    @RequestMapping("/findpIdByrId")
    public Result findpIdByrId( Integer id){
        try {
            List<Integer> list = roleService.findpIdByrId(id);
            return new Result(true,"查询权限成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,@RequestBody Role role){
        try {
            roleService.edit(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
        return new Result(true,"修改成功");
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"还有管理员正在使用改角色，请在删除前为管理员分配好对应角色");
        }
        return new Result(true,"删除成功");
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List <Role> list = roleService.findAll();
            return new Result(true,"查询角色成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }
}
